package com.cq.xjms.web.actions.sys;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.cq.xjms.model.Clazz;
import com.cq.xjms.model.Department;
import com.cq.xjms.model.Institute;
import com.cq.xjms.model.Student;
import com.cq.xjms.service.ClazzManager;
import com.cq.xjms.service.SysManager;
import com.cq.xjms.web.actions.BaseAction;

public class ClazzAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(ClazzAction.class);
	private ClazzManager clazzManager;
	private Department department;
	private Institute institute;
	private Clazz clazz;
	private String type;
	private List<Department> departments;
	private List<Clazz> clazzs;
	private List<Student> students;
	private List list=null;
	private SysManager sysManager;
	private List<Institute> institutes;
	private Map<String,Object> params = new HashMap<String,Object>();
	
	
	public String addClazz(){
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String saveClazz(){
		try{
			params.put("code",clazz.getCode());
			clazzs = sysManager.find(Clazz.CLAZZ_QUERY,params);
			if(clazzs==null || clazzs.size()==0){
				if(department.getId() !=null){
					clazz.setDepartment(department);
					sysManager.save(clazz);
					type="add";
				}else{
				    list = new ArrayList();
					list.add("请选择班级所在的系！");
					this.setActionErrors(list);
				}
			}else{
			     list = new ArrayList();
				list.add("此班级编号已经存在！");
				this.setActionErrors(list);
				
			}	
			
			
			
		}catch(Exception e){
			log.error("save user failed",e);
			return INPUT;
		}
		clazz = null;
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	/*public String getClazz(){
		return SUCCESS;
	}
	*/
	public String listClazzJson(){
		Map<String,Object> params = null;
		if(department!=null){
			params = new HashMap<String,Object>();
			params.put("id", department.getId());
		}
		clazzs = clazzManager.find(Clazz.CLAZZ_LIST, params);
		JSONArray arr = new JSONArray();
		for(Clazz d : clazzs){
			JSONObject obj = new JSONObject();
			obj.put("id", d.getId());
			obj.put("name", d.getName());
			obj.put("code", d.getCode());
			obj.put("departmentId", d.getDepartment().getId());
			obj.put("departmentName", d.getDepartment().getName());
			arr.add(obj);
		}
		this.writeJSON(arr.toString());
		return NONE;
	}

	public String list(){
		
		Map<String,Object> params = null;
		clazzs=new ArrayList<Clazz>();
		if(department!=null){
			if(department.getId()!=null){
				params = new HashMap<String,Object>();
				params.put("id", department.getId());
				clazzs = clazzManager.find(Clazz.CLAZZ_LIST, params);
			}else{
				
				   if(institute.getId()!=null){
						params = new HashMap<String,Object>();
						params.put("id", institute.getId());
						institutes = sysManager.find(Institute.INSTITUTE_QUERYBYID,params);
						   if(institutes.size()>=0){
				            	params =new HashMap<String,Object>();
					            	for(Institute i:institutes){
					      			   params.put("id", i.getId());
					      			   
					            	}
				          		departments = sysManager.find(Department.DEPARTMENT_QUERYById,params);
									 for(Department d:departments){
										    params = new HashMap<String,Object>();
										    params.put("id", d.getId());
										    List<Clazz> clazz = clazzManager.find(Clazz.CLAZZ_LIST, params);
										    for(Clazz c:clazz){
										    	clazzs.add((Clazz)c);
										    }
								     }
							}
	               }else{
	            	   clazzs = clazzManager.find(Clazz.CLAZZ_LIST_ALL, params);
	               }
			}
		}else{
			clazzs = clazzManager.find(Clazz.CLAZZ_LIST_ALL, params);
		}
		institutes = sysManager.find(Institute.INSTITUTE_LIST);
		return SUCCESS;
	}
	public String editClazz(){
		try{
			params.put("id", clazz.getId());
		    clazzs = sysManager.find(Clazz.CLAZZ_QUERY_BYID,params);
		    if(clazzs!=null && clazzs.size()>0){
		    	clazz = clazzs.get(0);
		    }
			institutes = sysManager.find(Institute.INSTITUTE_LIST);
			department = clazz.getDepartment();
			institute = department.getInstitute();
		}catch(Exception e){
			log.error("",e);
		}
		return SUCCESS;
	}
	public String editClazzSave(){
		
		try{
			log.info(clazz.getId());
			sysManager.saveOrUpdate(clazz);
		
		}catch(Exception e){
			log.error("update clazz failed !",e);
			return INPUT;
		}
		
		return SUCCESS;
	}

	public String delete(){
		params.put("clazz_id", clazz.getId());
		students=sysManager.find(Student.STUDENT_QUERYBYCLAZZID, params);
		if(students !=null&&students.size()>0){
			clazzs = sysManager.find(Clazz.CLAZZ_LIST_ALL);
			type="del";
			clazz = null;
			return INPUT;
		}
		sysManager.remove(Clazz.class, clazz.getId());
		return SUCCESS;
	}
	public void setclazzManager(ClazzManager clazzManager) {
		this.clazzManager = clazzManager;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Clazz> getClazzs() {
		return clazzs;
	}

	public void setClazzs(List<Clazz> clazzs) {
		this.clazzs = clazzs;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public SysManager getSysManager() {
		return sysManager;
	}

	public void setSysManager(SysManager sysManager) {
		this.sysManager = sysManager;
	}
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	public ClazzManager getClazzManager() {
		return clazzManager;
	}
	public void setClazzManager(ClazzManager clazzManager) {
		this.clazzManager = clazzManager;
	}
	public List<Institute> getInstitutes() {
		return institutes;
	}
	public void setInstitutes(List<Institute> institutes) {
		this.institutes = institutes;
	}
	public Institute getInstitute() {
		return institute;
	}
	public void setInstitute(Institute institute) {
		this.institute = institute;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Map<String, Object> getParams() {
		return params;
	}
	public void setParams(Map<String, Object> params) {
		this.params = params;
	}
	
	
	
	
}
